import type { FollowType } from '@/types/consult'
import { ref, onMounted, onUnmounted, type Ref } from 'vue'
import {
  followOrUnfollow,
  getPrescriptionPic,
  cancelOrder,
  deleteOrder
} from '@/services/consult'
import { showImagePreview, showToast } from 'vant'
import type { ConsultOrderItem } from '@/types/room'
import { OrderType } from '@/enums'
import { showFailToast, showSuccessToast, type FormInstance } from 'vant'
import { getMedicalOrderDetail } from '@/services/order'
import type { OrderDetail } from '@/types/order'
import { sendMobileCode } from '@/services/user'
import type { CodeType } from '@/types/user'
// 封装逻辑 规范起名useXxx, 表示使用某功能
export const useFollow = (type: FollowType = 'doc') => {
  // 关注功能
  const loading = ref(false)
  // 接受的类型数据可以少 传入的要比接受的类型多
  const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
    loading.value = true
    try {
      await followOrUnfollow(item.id, type)
      item.likeFlag = item.likeFlag === 1 ? 0 : 1
    } finally {
      loading.value = false
    }
  }
  return {
    loading,
    follow
  }
}
// 查看处方
export const useShowPrescription = () => {
  // 查看处方
  const showPrescription = async (id: string) => {
    if (id) {
      const res = await getPrescriptionPic(id)
      showImagePreview([res.data.url])
    }
  }
  return {
    showPrescription
  }
}

// 封装取消订单逻辑
export const useCancelOrder = () => {
  const loading = ref(false)
  const cancelConsultOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      await cancelOrder(item.id)
      item.status = OrderType.ConsultCancel
      item.statusValue = '已取消'
      showSuccessToast('取消成功')
    } catch (error) {
      showFailToast('取消失败')
    } finally {
      loading.value = false
    }
  }
  return { loading, cancelConsultOrder }
}

export const useDeleteOrder = (cb: () => void) => {
  // 删除订单
  const loading = ref(false)
  const deleteConsultOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      await deleteOrder(item.id)
      showSuccessToast('删除成功')
      // 成功，做其他业务
      cb && cb()
    } catch (e) {
      showFailToast('删除失败')
    } finally {
      loading.value = false
    }
  }
  return { loading, deleteConsultOrder }
}

export const useOrderDetail = (id: string) => {
  const loading = ref(false)
  const order = ref<OrderDetail>()
  onMounted(async () => {
    loading.value = true
    try {
      const res = await getMedicalOrderDetail(id)
      order.value = res.data
    } finally {
      loading.value = false
    }
  })
  return { order, loading }
}

export const useSendMobileCode = (
  mobile: Ref<string>,
  type: CodeType = 'login'
) => {
  const form = ref<FormInstance>()
  const time = ref(0)
  let timeId: number
  const send = async () => {
    // 如果倒计时时间大于0 ,此时不能发送验证码
    if (time.value > 0) return
    // 验证手机号对不对
    await form.value?.validate('mobile')
    console.log('验证通过可以发送验证码了')
    await sendMobileCode(mobile.value, type)
    showToast('发送成功')
    // 开启倒计时
    time.value = 60
    clearInterval(timeId)
    timeId = window.setInterval(() => {
      time.value--
      if (time.value <= 0) window.clearInterval(timeId)
    }, 1000)
  }
  onUnmounted(() => {
    window.clearInterval(timeId)
  })
  return {
    form,
    time,
    send
  }
}
